<?php

namespace App\Modules\Trade\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $table = 'orders';

    protected $fillable = ['distribution_channel_id', 'uid', 'created_at', 'product_id', 'price', 'status', 'pay_type', 'trade_no', 'pay_merchant_id', 'servicer', 'transaction_id', 'pay_end_at', 'create_ip', 'send_order_id', 'send_order_name', 'activity_id'];

    //获取渠道日数据
    static function getChannelDateOrderInfo($distribution_channel_id, $date)
    {
        $begin_time = $date;
        $end_time = date("Y-m-d", strtotime($begin_time) + 86400);
        return self::where('distribution_channel_id', $distribution_channel_id)
            ->where('created_at', '>=', $begin_time)
            ->where('created_at', '<', $end_time)
            ->where('status', 'PAID')
            ->sum('price');
    }

    //获取渠道日数据
    static function getChannelChargeAmount($distribution_channel_id, $begin_time, $end_time)
    {
        return self::where('distribution_channel_id', $distribution_channel_id)
            ->where('created_at', '>=', $begin_time)
            ->where('created_at', '<', $end_time)
            ->where('status', 'PAID')
            ->sum('price');
    }

    //获取付费用户数
    static function getPayUserNum($send_order_id)
    {
        return self::where('send_order_id', $send_order_id)
            ->where('status', 'PAID')
            ->distinct('uid')
            ->count('uid');
    }

    //总额统计
    static function getAmount($params = [])
    {
        $search_obj = self::where('status', 'PAID');
        if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) $search_obj->where('distribution_channel_id', $params['distribution_channel_id']);
        if (isset($params['begin_time']) && $params['begin_time']) $search_obj->where('created_at', '>=', $params['begin_time']);
        if (isset($params['end_time']) && $params['end_time']) $search_obj->where('created_at', '<=', $params['end_time']);
        if (isset($params['uid']) && $params['uid']) $search_obj->where('uid', $params['uid']);
        if (isset($params['send_order_id']) && $params['send_order_id']) $search_obj->where('send_order_id', $params['send_order_id']);
        if (isset($params['distribution_channel_id_array']) && $params['distribution_channel_id_array']) $search_obj->whereIn('distribution_channel_id', $params['distribution_channel_id_array']);
        if (isset($params['not_in_pay_merchant_id']) && count($params['not_in_pay_merchant_id']) > 0) {
            $search_obj->whereNotIn('pay_merchant_id', $params['not_in_pay_merchant_id']);
        } else {
        }
        return (float)$search_obj->sum('price');
    }

    static function getChargeAmountDistributionChannelId($distribution_channel_id, $begin_time, $end_time, $count)
    {
        $end_time = $end_time ? $end_time : date('Y-m-d H:i:s');
        $begin_time = $begin_time ? $begin_time : date('Y-m-d H:i:s');
        $sql = "SELECT SUM(price) as amount FROM send_orders JOIN orders ON send_orders.id=orders.send_order_id WHERE send_orders.distribution_channel_id=" . $distribution_channel_id . " AND orders.`status`='PAID' AND send_orders.send_time BETWEEN '" . $begin_time . "' AND '" . $end_time . "' AND (UNIX_TIMESTAMP(orders . created_at) - UNIX_TIMESTAMP(send_orders . send_time)) <=" . ($count * 86400);

        $data = DB::select($sql);
        //\Log::info($sql);
        return (float)$data[0]->amount;
    }

    static function getTodayData2($distribution_channel_ids)
    {
        $begin_time = date('Y-m-d');
        $search_obj = self::whereIn('distribution_channel_id', $distribution_channel_ids)
            ->where('created_at', '>=', $begin_time)->where('status', 'PAID');

        $search_obj->select(
            'order_type',
            DB::raw('sum(price) sum_price'),
            DB::raw('count(id) order_count'),
            DB::raw('count(distinct uid) user_count')
        );
        $search_obj->groupBy('order_type');
        $result = $search_obj->get();

        $year_order_num = 0;
        $normal_order_num = 0;
        $total_order_count = 0;
        $total_charge_amount = 0;
        foreach ($result as $item) {
            $total_charge_amount += $item->sum_price;
            $total_order_count += $item->order_count;
            if ($item->order_type == 'YEAR') {
                $year_order_num = $item->user_count;
            } else {
                $normal_order_num = $item->user_count;
            }
        }
        return compact('total_order_count', 'total_charge_amount', 'year_order_num', 'normal_order_num');
    }

    static function getPayUserCount($day, $distribution_channel_ids)
    {
        $search_obj = self::whereIn('distribution_channel_id', $distribution_channel_ids)
            ->where('created_at', '>=', $day)->where('status', 'PAID');
        return $search_obj->distinct('uid')->count();
    }

    static function getFansToDayOrderAmount($distribution_channel_ids)
    {
        $begin_time = date('Y-m-d');
        $search_obj = self::join('users', 'orders.uid', '=', 'users.id');
        $search_obj->whereIn('users.distribution_channel_id', $distribution_channel_ids);
        $search_obj->where('orders.status', 'PAID');
        $search_obj->where('users.created_at', '>=', $begin_time);
        return $search_obj->sum('orders.price');
    }
}
